#姓名：饶弟       学号：20160216048
from selenium import webdriver
from lxml import etree
import  twisted
import  scrapy
from openpyxl import Workbook
import time
class Douyu(object):
    def __init__(self):
        self.driver = webdriver.PhantomJS()

    def start(self):
        self.driver.get('https://www.douyu.com/directory/all')
        room_sum=0
        host_sum=0
        type_sum=0
        while True:
            time.sleep(2)
            # 姓名：饶弟       学号：20160216048
            content=etree.HTML(self.driver.page_source)
            roomnames=content.xpath("//div[@id='live-list-content']//h3[@class='ellipsis']/text()")
            hots=content.xpath("//div[@id='live-list-content']//span[@class='dy-num fr']/text()")
            names=content.xpath("//div[@id='live-list-content']//span[@class='dy-name ellipsis fl']/text()")
            types=content.xpath("//div[@id='live-list-content']//span[@class='tag ellipsis']/text()")
            for roomname,hot,name,type in zip(roomnames,hots,names,types):
                roomname=roomname.strip()
                print("\t热度数",hot,"    \t主播名:",name,"                   \t主播类型：",type,"          \t房间名：",roomname)
                room_sum+=1
                if hot[-1]=='万':
                    hot=hot[:-1]
                    hot=int(float(hot)*10000)
                    host_sum+=hot
                     #host_sum=host_sum+hot

                else:
                    host_sum+=int(hot)

                if type=='绝地求生':
                    type_sum+=1
                else:
                    a=0
                    a+=1
            ret=self.driver.page_source.find('shark-pager-disable-next')
            if ret>0:
                break
            else:
                # 非最后一页，点击下一页
                self.driver.find_element_by_class_name('shark-pager-next').click()
        print('房间总数：',room_sum)
        print('热度总数：', host_sum)
        print('主播名总数：', room_sum)
        print('绝地求生主播总数：',type_sum)
        print('\n\n\n\n   #姓名：饶弟       学号：20160216048')



class DoubanPipeline(object):
    wb = Workbook()
    ws = wb.active
    # 设置表头
    ws.append(['标题', '评分'])

    def process_item(self, item):
        # 添加数据
        line = [item['title'], item['star']]
        self.ws.append(line)  # 按行添加
        self.wb.save('douban.xlsx')
        return item


if __name__=="__main__":
    douyu=Douyu()
    douyu.start()





#//div[@id='live-list-content']//h3[@class='ellipsis']/text()
#//div[@id='live-list-content']//span[@class='dy-num fr']/text()
#ret= driver.page_source.find('shark-pager-disable-next')
#print(ret)
